﻿using DevExpress.XtraEditors;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;

namespace Aker.MainForms
{
    public partial class ReportViewer : XForm
    {

        public XtraReport Report;

        public ReportViewer(XtraReport report, string email, string caption)
        {
            InitializeComponent();
            this.Report = report;
            txtEmail.EditValue = email;
            this.Text = caption;

        }

        private void ReportViewer_Load(object sender, EventArgs e)
        {
            docViewer.DocumentSource = Report;
            Report.CreateDocument();

            //lblMail.Text = Language.ReportViewer.ClientMail;
            //btnSendEmail.Text = Language.ReportViewer.SendMail;

        } 

        private void btnEpostaGonder_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MailMessage email = new MailMessage();
            email.From = new MailAddress("fair@akertekstil.com", "Aker Tekstil Kumascilik");
            email.Subject = "Aker Order Report";
            email.Body = "Aker Order Report attached ...";

            email.IsBodyHtml = true;
            email.To.Add(txtEmail.EditValue.ToString());

            string FileName = "AkerOrderReport." + DateTime.Now.ToString("yyyyMMhhmmss") + ".pdf";
            string FilePath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + "\\" + FileName;
            Report.ExportToPdf(FilePath);
            Stream stream = new MemoryStream(File.ReadAllBytes(FilePath));
            email.Attachments.Add(new Attachment(stream, FileName));

            try
            {
                using (SmtpClient emailClient = new SmtpClient("smtp.office365.com", 587))
                {
                    emailClient.Credentials = new NetworkCredential("fair@akertekstil.com", "Dajo8509");
                    emailClient.EnableSsl = true;
                    emailClient.Send(email);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("e-mail can not be send. Check the internet connection." + ex.ToString() + "...");
            }

            XtraMessageBox.Show("e-mail sent successfully.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string sipNo = this.Text.Replace("Sipariş No:", "");
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.Filter = "*.pdf|PDF Dosyaları";
                sfd.FileName = "AKR-POrder." + sipNo + ".pdf";
                sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    Report.ExportToPdf(sfd.FileName);
                }
            }
        }


    }
}
